
//二级菜单划过效果
var pp = $(".chang").children();

pp.mouseenter(function(){
	
	 $(this).css({
		 background : "blue"
	 })
})
pp.mouseleave(function(){
	
	$(this).css({
		background : "deeppink"
	})
})

	
//----------------------------banner图----------------------------------------
var mySwiper = new Swiper('#swiper-container1', {
	    effect : 'flip',
		loop: true,
		autoplay: true,
		navigation: {
			nextEl: '.swiper-button-next',
			prevEl: '.swiper-button-prev',
		  },
		  autoplay: {
			disableOnInteraction: false,
		  },
	})

//-------------------------商品列表渲染-----------------------------------------
$.getJSON("http://localhost:3000/pdd/index.json", function(rest) {
	res = rest[0];
	//console.log(res);
	rem = rest[1];
	//console.log(rem);
	var content_groups = document.querySelector(".content-groups");
	for (var i = 0; i < rem.length; i++) {
		var htmla =
			`<a href="javascript:void(0)" class="subject-wrap">
						<img src=${rem[i].ch_ul} alt="" class="subject img-thumbnail">
						<div class="goods-group">
			
						</div>
					</a>`
		content_groups.innerHTML += htmla;

	}
	var goods_group = document.querySelectorAll(".goods-group");
	for (var k = 0; k < goods_group.length; k++) {
		for (var i = 0; i < 3; i++) {
			var html =
				`<div class="goods">
							
						</div>`
			goods_group[k].innerHTML += html;
		}
	}
	var goods=document.querySelectorAll(".goods");
	
    for(var i=0;i<goods.length;i++){
		
		var htmlc=`<img src=${res[i].hd_thumb_url} alt="" class="img img-thumbnail">
						<div class="title">${res[i].goods_name}</div>
						<div class="price">
							<span class="group-price">$${res[i].normal_price/100}</span>
							<span class="market-price">$${res[i].market_price/100}</span>
							<span class="shop-car">加入购物车</span>
						</div>`
		goods[i].innerHTML += htmlc;
						
	}
	
	//获取localstorage,如果为空则返回一个空数组，不为空则返回value
	var car_list=(function(){
		var storage=localStorage.getItem("cctv");
		//console.log(storage);
	    if(storage === null){
			localStorage.setItem("cctv","[]")
			return []
		}
		return JSON.parse(storage);
		
	})();
	//-------------------事件委托可以父级也可以是爷爷级--------------------------
	
	$(".goods-group").on("click",".shop-car",function(){
	
		var car_index=$(this).index(".shop-car")
		var item=res[car_index]
		console.log(item)
	//点击shop-car时，遍历数组，如果点击的ID等于数组的ID则count+1,如果不等就把点击的数据添加进数组
		var has_som=car_list.some(function(carts_item){
            if(carts_item.goods_id===item.goods_id){
				carts_item.count++;
				return true;
			}	
		})
          	
		if(!has_som){
		    item.count=1;
			car_list.push(item);
		}
	 
		//------------设置localstorage-----------
	   localStorage.setItem("cctv",JSON.stringify(car_list));
	   //console.log(car_list.length)
	//购物车后面的数字
	var benchi=0;
	for(let i=0;i<car_list.length;i++){
		
		benchi += car_list[i].count
  
	}
	$(".cartsb_count").html(benchi);
	})
	//购物车后面的数字
	var benchi=0;
	for(let i=0;i<car_list.length;i++){
		
		benchi += car_list[i].count
  
	}
	$(".cartsb_count").html(benchi);
	//-----------------------跳转购物车页面----------------------------------------
	$(".sp").click(function(){
		location.assign("./shop_car.html")
	})
	//-----------------------详情放大镜--------------------------------
	$(".goods-group").on("click",".img",function(){
		var index=$(this).index(".img");
		console.log(res,index);
		var loca_goods=JSON.stringify(res[index]);
	    localStorage.setItem("chang",loca_goods);
		location.assign("http://localhost:3000/fangdajing.html")
	})
	
	
})
//------------------------------吸顶----------------------------------------
console.log($(".return"))	
$(window).scroll(function(){
		var $scroll_top=$("html,body").scrollTop();
		//console.log($(".title-groups").offset())
		//console.log($scroll_top)
		if($scroll_top>=1079){
			$(".title-groups").css({
				position:"fixed",
				top:103
			})
		}else{
			$(".title-groups").css({
				position: "absolute",
				top:1184
			})
		}
		if($scroll_top>=3100){
			$(".return").css({
				display:"block"
			})
		}else{
			$(".return").css({
				display:"none"
			})
		}
	})
//---------------------------------返回顶部--------------------------------------------------
$(".return").click(function(){
	$("html,body").scrollTop(0)
})
//------------------------cookie免登录功能------------------------------------------

var cookies=JSON.parse($.cookie("USRID"))
//console.log(cookies);
if(cookies != null){
	$(".navbar-right").css({
		display:"block"
	})
	$("#user_name").html(cookies.username)
}else{
	$(".navbar-right").css({
		display:"none"
	})
}
$("#signout").click(function(){
	$.cookie('USRID', null);
	location.reload();
})
//console.log(cookies);